home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 45 / Amiga Format CD45 (1999-09)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-11].iso / -serious- / misc / mcread / original / main.c < prev    next >
C/C++ Source or Header  |  1999-08-09  |  4KB  |  197 lines

  1. /*    mcread: main.c
  2.     Copyright (C) 1991, Mike Gleason Jr & NCEMRSoft.
  3.     All Rights Reserved. See mcread.h for details. */
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <signal.h>
  8.  
  9. #ifndef THINK_C
  10. #ifndef applec
  11. #ifdef BIG_ENDIAN
  12. #include <sys/types.h>    /* need sys/types.h or stddef.h for size_t */
  13. #endif
  14. #endif
  15. #endif                            /* if unix */
  16.  
  17. #include "mcread.h"
  18.  
  19. extern short                    tab, width, linecount, pager;
  20. short                            readFromStdin;
  21.  
  22.  
  23. main (argc, argv)
  24.     int argc;
  25.     char **argv;
  26. {
  27.     register short i, filesToDo, filesDone;
  28.     char crap[80];
  29.     extern void on_interrupt();
  30.         
  31. #ifdef THINK_C
  32. #include <console.h>
  33.     argc = ccommand (&argv);
  34. #endif
  35.     if (argc < 2) {
  36. usage:
  37.         (void) fprintf (stderr, "usage: %s %s\n", argv[0], USAGE);
  38.         exit (EXIT_FAILURE);
  39.     }
  40.  
  41.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  42.         (void) signal(SIGINT, on_interrupt);
  43.  
  44.     readFromStdin = 0;
  45.  
  46.     /* preprocess options... */
  47.     for (i=1,filesToDo=0; i<argc; i++)
  48.         if (*argv[i] == '-')
  49.             switch (argv[i][1]) {
  50.                 case 't': tab = atoi (argv[i] + 2); break;
  51.                 case 'w': width = atoi (argv[i] + 2); break;
  52.                 case 'p':
  53.                     if (argv[i][2])
  54.                         pager = atoi (argv[i] + 2);
  55.                     else
  56.                         pager = 23;
  57.                     break;
  58.                 /* case 's':
  59.                 case '\0': readFromStdin = 1; break; */
  60.                 default:
  61.                     goto usage;
  62.             }
  63.         else
  64.             filesToDo++;
  65.  
  66.     /* ...now do the converting. */
  67.     if (readFromStdin)
  68.         (void) thrash ("stdin");
  69.     else for (i=1,filesDone=0; i<argc; i++)
  70.         if (*argv[i] != '-') {
  71.             linecount = 0;
  72.             if (filesToDo > 1)
  73.             {
  74.                 (void) printf ("\n\nfile: %s\n", argv[i]);
  75.                 linecount = 1;
  76.                 (void) thrash (argv[i]);
  77.                 filesDone++;
  78.                 if (pager > 0 && filesDone < filesToDo) {
  79.                     (void) printf ("...end of file '%s'...hit return, q to cancel...", argv[i]);
  80.                     (void) fgets (crap, 78, stdin);
  81.                     if (*crap == 'q') exit (EXIT_SUCCESS);
  82.                 }
  83.             } else (void) thrash (argv[i]);
  84.         }
  85. }    /* main */
  86.  
  87.  
  88.  
  89. void on_interrupt()
  90. {
  91.     (void) fprintf (stderr, "### mcread: aborted.\n");
  92.     exit (EXIT_FAILURE);
  93. }    /* on_interrupt */
  94.  
  95.  
  96.  
  97. int thrash (name)
  98.     char                    *name;
  99. {
  100.     char                    typestr[8];
  101.     long                    dforklen;
  102.     FILE                    *in;
  103.     int                        result;
  104.     
  105.     if (readFromStdin)
  106.         return (0);        /* not supported yet */
  107.         
  108.     if (!(in = fopen (name, READ_BINARY))) {
  109.         (void) fprintf (stderr, "%s: could not open for reading.\n", name);
  110.         return (1);
  111.     }
  112.     
  113.     dforklen = CheckMBHeader(in, typestr);
  114.     
  115.     if (dforklen < 0L) {        /* no mb header.  gr.... */
  116.         result = thrash_macwrite (in, name, dforklen);
  117.         if (result) {        /* not macwrite, try text. */
  118.             rewind (in);
  119.             result = thrash_text (in, name, dforklen, TRUE);
  120.         }
  121.     }    
  122.     else if (strcmp(MACWRITE_TYPE, typestr) == 0)
  123.         result = thrash_macwrite (in, name, dforklen);
  124.     else if (strcmp(TEXT_TYPE, typestr) == 0)
  125.         result = thrash_text (in, name, dforklen, FALSE);
  126.     else if (strcmp(TEACHTEXT_TYPE, typestr) == 0)
  127.         result = thrash_text (in, name, dforklen, TRUE);
  128.     else if (strcmp(WORD_TYPE, typestr) == 0) {
  129.         (void) fprintf (stderr, "%s: cannot mcread MS Word documents (yet).\n",
  130.         name);
  131.     }
  132.     else {
  133.         (void) fprintf (stderr, "%s: unrecognized file type (%s)\n",
  134.             name, typestr);
  135.         (void) fclose (in);
  136.         return (2);
  137.     }
  138.     
  139.     (void) fclose (in);
  140.     return (0);
  141. }    /* thrash;  am I the only programmer who listens to thrash/speed metal? */
  142.  
  143.  
  144.  
  145. /* If you know for sure that the machine you are going to run this on had
  146.     Big Endian byte-ordering, you can #define it and you'll get a mild
  147.     speed increase/size decrease. */
  148.     
  149. #ifdef BIG_ENDIAN
  150.  
  151. MacWord Getw (in)
  152.     FILE *in;
  153. {
  154.     MacWord aWord;    /* 2 bytes */
  155.     fread (&aWord, (size_t) 2, (size_t) 1, in);
  156.     return (aWord);
  157. }
  158.  
  159. MacLongword Getl (in)
  160.     FILE *in;
  161. {
  162.     MacLongword aLongword;    /* 4 bytes */
  163.     fread (&aLongword, (size_t) 4, (size_t) 1, in);
  164.     return (aLongword);
  165. }
  166.  
  167. #else
  168.  
  169. MacWord Getw (in)
  170.     FILE *in;
  171. {
  172.     register MacWord a, b;    
  173.     register MacWord aWord;    /* 2 bytes */
  174.     
  175.     a = (MacWord) getc (in) << 8;
  176.     b = (MacWord) getc (in);
  177.     aWord = a + b;
  178.     return (aWord);
  179. }
  180.  
  181. MacLongword Getl (in)
  182.     FILE *in;
  183. {
  184.     register MacLongword aLongword;    /* 4 bytes */
  185.     register MacLongword a, b, c, d;    
  186.     
  187.     a = (long) getc (in) << 24;
  188.     b = (long) getc (in) << 16;
  189.     c = (long) getc (in) << 8;
  190.     d = (long) getc (in);
  191.     aLongword = a + b + c + d;
  192.     return (aLongword);
  193. }
  194.  
  195. #endif
  196. /* eof */
  197.